<script lang="ts" setup>
import type { AiTrainingDatasetMetaApi } from '#/api/ai/dataset/trainingDatasetMeta';

import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { createVersionFast } from '#/api/ai/dataset/trainingDatasetMeta';
import { $t } from '#/locales';

import { useCreateVersionFastSchema } from '../data';

const emit = defineEmits(['success']);
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 100,
  },
  wrapperClass: 'grid-cols-1',
  layout: 'horizontal',
  schema: useCreateVersionFastSchema(),
  showDefaultActions: false,
});

const [Modal, modalApi] = useVbenModal({
  closeOnClickModal: false,
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = await formApi.getValues();
    try {
      await createVersionFast(data.id, data.versionNum, data.auditStatus);

      // 全部导入成功
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    // 加载数据
    const data =
      modalApi.getData<AiTrainingDatasetMetaApi.TrainingDatasetMeta>();
    data.versionNum = undefined;
    await formApi.setValues(data);
  },
});
</script>

<template>
  <Modal title="快速复制数据集版本" class="w-1/3">
    <Form class="mx-4" />
  </Modal>
</template>
